package com.bsj.travel.openapi.web;

import com.bsj.travel.def.common.DTO.web.openApi.device.DeviceOpenApiDTO;
import com.bsj.travel.def.common.DTO.web.openApi.device.DeviceOpenApiMsgReportDTO;
import com.bsj.travel.def.common.DTO.web.openApi.device.DeviceOpenApiParamDTO;
import com.bsj.travel.def.common.VO.device.DeviceCorrelationInfoVO;
import com.bsj.travel.def.global.JsonResult;
import com.bsj.travel.def.global.JsonResultEnum;
import com.bsj.travel.openapi.service.DeviceOpenApiService;
import com.bsj.travel.security.auth.AuthOpenApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.TreeMap;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description DeviceOpenApiController
 * @time 2024/4/7 14:41
 */
@Slf4j
@RestController
@RequestMapping(value = "/openApi/device")
public class DeviceOpenApiController {

    @Autowired
    private DeviceOpenApiService deviceOpenApiService;

    @Autowired
    private AuthOpenApi authOpenApi;

    /**
     * 添加设备
     *
     * @param deviceOpenApiDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/4/7 14:46
     */
    @PostMapping("/add")
    public JsonResult add(HttpServletRequest request, @Validated @RequestBody DeviceOpenApiDTO deviceOpenApiDTO) {
        log.info("请求参数{}", deviceOpenApiDTO);
        String productKey = deviceOpenApiDTO.getProductKey();
        TreeMap<String, String> map = authOpenApi.getCommonParam(request);
        authOpenApi.signCheck(map, productKey);
        return deviceOpenApiService.addDevice(deviceOpenApiDTO);
    }

    /**
     * 批量添加设备
     *
     * @param deviceOpenApiParamDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/4/8 11:16
     */
    @PostMapping("/batchAdd")
    public JsonResult batchAdd(HttpServletRequest request, @Validated @RequestBody DeviceOpenApiParamDTO deviceOpenApiParamDTO) {
        String productKey = deviceOpenApiParamDTO.getProductKey();
        List<DeviceOpenApiDTO> devices = deviceOpenApiParamDTO.getDevices();
        if (CollectionUtils.isEmpty(devices)) {
            return JsonResult.fail(JsonResultEnum.DEVICE_INFO_IS_NULL);
        }
        TreeMap<String, String> map = authOpenApi.getCommonParam(request);
        authOpenApi.signCheck(map, productKey);
        return deviceOpenApiService.batchAddDevice(deviceOpenApiParamDTO);
    }

    /**
     * 删除设备
     *
     * @param request
     * @param deviceOpenApiParamDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/4/8 11:53
     */
    @PostMapping("/delete")
    public JsonResult delete(HttpServletRequest request, @Validated @RequestBody DeviceOpenApiParamDTO deviceOpenApiParamDTO) {
        String productKey = deviceOpenApiParamDTO.getProductKey();
        List<String> deviceNames = deviceOpenApiParamDTO.getDeviceNames();
        if (CollectionUtils.isEmpty(deviceNames)) {
            return JsonResult.fail(JsonResultEnum.DEVICE_NAME_IS_NULL);
        }
        TreeMap<String, String> map = authOpenApi.getCommonParam(request);
        authOpenApi.signCheck(map, productKey);
        return deviceOpenApiService.deleteDevice(deviceOpenApiParamDTO);
    }

    /**
     * 设备详情
     *
     * @param request
     * @param deviceOpenApiParamDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/4/8 13:58
     */
    @PostMapping("/queryDeviceDetail")
    public JsonResult queryDeviceDetail(HttpServletRequest request, @Validated @RequestBody DeviceOpenApiParamDTO deviceOpenApiParamDTO) {
        String deviceName = deviceOpenApiParamDTO.getDeviceName();
        if (StringUtils.isBlank(deviceName)) {
            return JsonResult.fail(JsonResultEnum.DEVICE_NAME_IS_NULL);
        }
        String productKey = deviceOpenApiParamDTO.getProductKey();
        TreeMap<String, String> map = authOpenApi.getCommonParam(request);
        authOpenApi.signCheck(map, productKey);
        DeviceCorrelationInfoVO deviceCorrelationInfoVO = deviceOpenApiService.getByDeviceName(productKey, deviceName);
        return JsonResult.success(deviceCorrelationInfoVO);
    }

    /**
     * 批量获取设备详情
     *
     * @param request
     * @param deviceOpenApiParamDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/4/8 14:27
     */
    @PostMapping("/batchQueryDeviceDetail")
    public JsonResult batchQueryDeviceDetail(HttpServletRequest request, @Validated @RequestBody DeviceOpenApiParamDTO deviceOpenApiParamDTO) {
        List<String> deviceNames = deviceOpenApiParamDTO.getDeviceNames();
        if (CollectionUtils.isEmpty(deviceNames)) {
            return JsonResult.fail(JsonResultEnum.DEVICE_NAME_IS_NULL);
        }
        String productKey = deviceOpenApiParamDTO.getProductKey();
        TreeMap<String, String> map = authOpenApi.getCommonParam(request);
        authOpenApi.signCheck(map, productKey);
        List<DeviceCorrelationInfoVO> deviceCorrelationInfoVOS = deviceOpenApiService.batchQueryDeviceDetail(productKey, deviceNames);
        return JsonResult.success(deviceCorrelationInfoVOS);
    }

    /**
     * 消息发布
     *
     * @param request
     * @param deviceOpenApiMsgReportDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/4/8 15:00
     */
    @PostMapping("/pub")
    public JsonResult pub(HttpServletRequest request, @Validated @RequestBody DeviceOpenApiMsgReportDTO deviceOpenApiMsgReportDTO) {
        String productKey = deviceOpenApiMsgReportDTO.getProductKey();
        TreeMap<String, String> map = authOpenApi.getCommonParam(request);
        authOpenApi.signCheck(map, productKey);
        return deviceOpenApiService.pub(deviceOpenApiMsgReportDTO);
    }

}
